<script setup lang="ts">
import RoomStatus from './components/RoomStatus.vue'
import RoomAction from './components/RoomAction.vue'
import RoomMessage from './components/RoomMessage.vue'

import type { Socket } from 'socket.io-client'
import { io } from 'socket.io-client'
import { nextTick, onMounted, onUnmounted, ref } from 'vue'
import { baseURL } from '@/utils/http'
import { useUserStore } from '@/stores'
import { useRoute } from 'vue-router'
import { MsgType, OrderType } from '@/enums'

import type { Message, TimeMessages } from '@/types/room'
import type { Image } from '@/types/consult'
import type { OrderItem } from '@/types/order'
import dayjs from 'dayjs'

import { payOrderDetailApi } from '@/services/order'
import { showToast } from 'vant'

import { provide } from 'vue'

const consult = ref<OrderItem>()
provide('consult', consult)

const list = ref<Message[]>([])

const store = useUserStore()
const route = useRoute()

const initialMsg = ref(true)
const loading = ref(true)

let socket: Socket

onUnmounted(() => {
  socket.close()
})

const time = ref(dayjs().format('YYYY-MM-DD HH:mm:ss'))

onMounted(async () => {
  const res = await payOrderDetailApi({
    orderId: route.query.orderId as string
  })
  consult.value = res.data
  // 建立链接，创建 socket.io 实例
  socket = io(baseURL, {
    auth: {
      token: `Bearer ${store.user?.token}`
    },
    query: {
      orderId: route.query.orderId
    }
  })

  socket.on('connect', () => {
    // 建立连接成功
    list.value = []
    console.log('connect')
  })

  socket.on('error', () => {
    // 错误异常消息
    console.log('error')
  })

  socket.on('disconnect', () => {
    // 已经断开连接
    console.log('disconnect')
  })

  socket.on('chatMsgList', ({ data }: { data: TimeMessages[] }) => {
    loading.value = false
    if (!data.length) return showToast('没有聊天记录了')
    // 准备转换常规消息列表
    const arr: Message[] = []
    data.forEach((item, i) => {
      if (i === 0) time.value = item.createTime
      arr.push({
        msgType: MsgType.Notify,
        msg: { content: item.createTime },
        createTime: item.createTime,
        id: item.createTime
      })
      arr.push(...item.items)
      if (!data.length) {
        return showToast('没有聊天记录了')
      }
      nextTick(() => {
        if (initialMsg.value) {
          initialMsg.value = false
          socket.emit('updateMsgStatus', arr[arr.length - 1].id)
          window.scrollTo(0, document.body.scrollHeight)
        }
      })
    })
    // 追加到聊天消息列表
    list.value.unshift(...arr)
  })

  socket.on('statusChange', async () => {
    const res = await payOrderDetailApi({
      orderId: route.query.orderId as string
    })
    consult.value = res.data
  })

  socket.on('receiveChatMsg', async (event) => {
    list.value.push(event)
    await nextTick()
    socket.emit('updateMsgStatus', event.id)
    window.scrollTo(0, document.body.scrollHeight)
  })
})

const sendText = (text: string) => {
  // 发送信息需要  发送人  收消息人  消息类型  消息内容
  socket.emit('sendChatMsg', {
    from: store.user?.id,
    to: consult.value?.docInfo?.id,
    msgType: MsgType.MsgText,
    msg: { content: text }
  })
}

const sendImage = (img: Image) => {
  socket.emit('sendChatMsg', {
    from: store.user?.id,
    to: consult.value?.docInfo?.id,
    msgType: MsgType.MsgImage,
    msg: { picture: img }
  })
}

const onRefresh = () => {
  loading.value = true
  socket.emit('getChatMsgList', 20, time.value, route.query.orderId)
}

const completeEva = (score: number) => {
  const item = list.value.find((item) => item.msgType === MsgType.CardEvaForm)
  if (item) {
    item.msg.evaluateDoc = { score }
    item.msgType = MsgType.CardEva
  }
}
provide('completeEva', completeEva)
</script>

<template>
  <div class="room-page">
    <cp-nav-bar title="医生问诊室" />
    <room-status :status="consult?.status" :countdown="consult?.countdown" />
    <room-message :list="list" :loading="loading" @refresh="onRefresh" />
    <room-action
      :disabled="consult?.status !== OrderType.ConsultChat"
      @send-text="sendText"
      @send-image="sendImage"
    />
  </div>
</template>

<style lang="scss" scoped>
.room-page {
  padding-top: 90px;
  padding-bottom: 60px;
  min-height: 100vh;
  box-sizing: border-box;
  background-color: var(--cp-bg);
  .van-pull-refresh {
    width: 100%;
    min-height: calc(100vh - 150px);
  }
}
</style>
